#Test script of Deal Smoke Test
#require File.dirname(__FILE__)+'/../lib/testdata.rb'
#require File.dirname(__FILE__)+'/../lib/elements/Sales/element_OrgSearch.rb'
require File.dirname(__FILE__)+'/../lib/elements/element_Deal.rb'
require File.dirname(__FILE__)+'/../lib/elements/element_Org.rb'
require File.dirname(__FILE__)+'/../lib/reporting.rb'
require File.dirname(__FILE__)+'/../lib/std_lib.rb'
require 'watir'
require 'watir/ie'
require 'watir/contrib/enabled_popup'
require 'watir/testcase'

class Smoke_Test_Deal < Watir::TestCase
    # open INT or QA
    # CRM_STD_LIB.login($int,"tony2008","2165270")
    # CRM_STD_LIB.open_page("Org Search")
    $ie = Watir::IE.attach(:title,"Tonytest")
    $ie.bring_to_front
    def test_setup
        filename  = File.basename(__FILE__).gsub(/\.rb/, "")
        begin
            $log=Reporting.new(File.dirname(__FILE__)+'/../doc/report/'"",filename)
            @@dealname = "Tonytest_"+Time.now.strftime("%Y%m%d")+"_deal"
            #            $ie = Watir::IE.attach(:title,"Tonytest")
            #            $ie.bring_to_front
        rescue
            puts $!
            puts $@
            $log.close unless $log.nil?
        end
    end
    # Add new deal to an Org
    def test_add_new_deal
        $log.test_notes("DealSmokeTest-INT")
        #$log.test_notes("OrgSmokeTest-QA")
        $log.test_notes("Start to Test Add New Deal")
        Element::OrgHomePage.contacts_and_deals_link.click
        Element::OrgHomePage.add_deal_button.click
        sleep 1
        def count_contact
            @array = []
                if $ie.checkbox(:name=>"ci").exist?
                    for i in 0..100
                        if $ie.checkbox(:name=>"ci",:index=>i+1).exist?
                            i+=1
                            @array << i
                        else
                            break
                        end
                        return @array[rand(i)]
                    end
                else
                    return 0
                end
            
            end
            @@contactnum = count_contact

            Element::CreateDeal.deal_name_text_field.set(@@dealname)
            Element::CreateDeal.deal_product_family_select_list.select("ACC")
            #Element::CreateDeal.deal_market_select_list.select("")
            Element::CreateDeal.deal_primary_status_select_list.select(/Closed/)
            Element::CreateDeal.deal_close_date_text_field.set($date)
            if @@contactnum !=0
                Element::CreateDeal.deal_contacts_checkbox(@@contactnum).set
            end
            Element::CreateDeal.deal_add_button.click_no_wait
            CRM_STD_LIB.popup_clicker($ie,"OK")
            sleep 5
            $log.test_validate(Element::CreateDeal.deal_name_text_field.value.include?(@@dealname),"Deal Name correct or not")
            $log.test_validate(Element::CreateDeal.deal_product_family_select_list.text.include?("ACC"),"Product Family Name correct or not")
            $log.test_validate(Element::CreateDeal.deal_primary_status_select_list.text.include?("Closed (100%)"),"Primary Status correct or not")
            $log.test_validate(Element::CreateDeal.deal_close_date_text_field.value.include?($date),"Close Date correct or not")

        end
# Try one contact ????
        def test_reset

        end

        def test_tear_down
            @@for = nil
            @@search_in = nil
        end


        class NewOrg < Watir::TestCase
            def test_setup
            end

            def test_tear_down
                $log.close unless $log.nil?
            end
        end

    end


